//
//  InHistoryViewController.m
//  Inner  Voice
//
//  Created by 徐海宇 on 16/2/29.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InHistoryViewController.h"

#import "InSoundTool.h"

#import "InHistoryTableViewCell.h"

#import "AppDelegate.h"

#import "InPlayerViewController.h"

#import "InSound.h"

#define kIdentifier @"historyCell"


@interface InHistoryViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *InSoundArray;

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation InHistoryViewController


- (NSMutableArray *)InSoundArray {
    //数组为本地数据库的记录
    if (_InSoundArray == nil) {
        if ([[NSFileManager defaultManager] fileExistsAtPath:[InSoundTool sadBox]]) {
            _InSoundArray = [InSoundTool soundArray];
        }else {
            _InSoundArray = [NSMutableArray array];

        }
    }
    return _InSoundArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
  
    
    
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"InHistoryTableViewCell" bundle:nil] forCellReuseIdentifier:kIdentifier];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
}
- (IBAction)backBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)start:(id)sender {
    InPlayerViewController *contro = [[InPlayerViewController alloc]init];
    [self presentViewController:contro animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.InSoundArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier forIndexPath:indexPath];
    
    InSound *sound = self.InSoundArray[indexPath.row];
    
    cell.soundNameLabel.text = sound.inSound_name;
     [cell loadImageWithStr:sound.inSound_icon];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}


//删除按钮的标题

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"删除";
    
}

//这里就是点击删除执行的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    InSound *sound = self.InSoundArray[indexPath.row];
    
    BOOL success = [InSoundTool deleteSound:sound];
    if (success) {
        [self.InSoundArray removeObjectAtIndex:indexPath.row];
        
        [self.myTableView reloadData];
        NSLog(@"删除成功");
        
    }else {
        NSLog(@"删除失败");
    }
}

//点击后的事件没写
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    
    InSound *sound = self.InSoundArray[indexPath.row];//得到当前点击的歌曲的信息
    NSURL *url = [NSURL URLWithString:sound.inSound_Url];
    
    [delegate.InPlayerViewController.inplayer playWithUrl:url showView:delegate.InPlayerViewController.sliderView];//播放该歌曲的信息
    
    delegate.InPlayerViewController.index = indexPath.row;//将歌曲的index传给播放器页面
    
    
    delegate.InPlayerViewController.inSound = sound;
    delegate.InPlayerViewController.playingSoundArray = self.InSoundArray;//播放器正在播放的数组为历史播放记录的数组
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
